package org.fing.edu.uy.esbadp.motor.comunication;

import java.util.List;
import java.util.logging.Level;

import org.fing.edu.uy.esbadp.core.constants.ConfigConstants;
import org.fing.edu.uy.esbadp.core.constants.JMXServiceConstants;
import org.fing.edu.uy.esbadp.core.entity.DMRegisteredService;
import org.fing.edu.uy.esbadp.core.utils.JMXManager;
import org.fing.edu.uy.esbadp.logger.LoggerADP;
import org.fing.edu.uy.esbadp.motor.config.MgrConfig;

public class AdmServiceComunication {
	
	 private static java.util.logging.Logger logger = LoggerADP.getLogger(AdmServiceComunication.class.getSimpleName());
	
	
	/**
	 * Funcion encargada de obtener el servicio a partir de un id de servicio.
	 * @param serviceId Id del servicio.
	 * @return DMRegisteredService.
	 * @throws Exception En caso de error al invocar la operacion JMX retorna error.
	 */
	public static DMRegisteredService getVirtualService(Long serviceId) {
		try{
			//config call operations getEquivalentsServices
			String jmxServiceURL = MgrConfig.getInstance().getProperty(ConfigConstants.JMXEsbAdpURL);
			String mBeanName = JMXServiceConstants.JMXServicesAdminMBean;
			Object[] arguments = {serviceId};
			String[] signature = {Long.class.getName()};
			Object result = JMXManager.callOperation(jmxServiceURL, mBeanName, "getVirtualService" , arguments, signature);
			if(result != null){
				return (DMRegisteredService) result;
			}
		}catch (Exception ex) {
			logger.log(Level.SEVERE, "Exception when callOperation " + "getVirtualService" + ".", ex);
		}
		return null;
	}
	
	
	/**
	 * Funcion encargada de obtener los servicios de queivalente a partir de un id de servicio.
	 * @param serviceId Id del servicio.
	 * @return List Retorna una lista con los servicios equivalentes.
	 * @throws Exception En caso de error al invocar la operacion JMX retorna error.
	 */
	@SuppressWarnings("unchecked")
	public static List<DMRegisteredService> getEquivalentsServices(Long serviceId) {
		try{
			//config call operations getEquivalentsServices
			String jmxServiceURL = MgrConfig.getInstance().getProperty(ConfigConstants.JMXEsbAdpURL);
			String mBeanName = JMXServiceConstants.JMXServicesAdminMBean;
			Object[] arguments = {serviceId};
			String[] signature = {Long.class.getName()};
			
			//get equivalents services
			Object result = JMXManager.callOperation(jmxServiceURL, mBeanName, "getEquivalentsServices", arguments, signature);
			if(result != null){
				return (List<DMRegisteredService>) result;
			}
		}catch (Exception ex) {
			logger.log(Level.SEVERE, "Exception when callOperation getEquivalentsServices" + ".", ex);
		}
		return null;
	}
	
	
	public static String getToCanonicalXSL(Long serviceId) {
		try{
			String jmxServiceURL = MgrConfig.getInstance().getProperty(ConfigConstants.JMXEsbAdpURL);
			String mBeanName = JMXServiceConstants.JMXServicesAdminMBean;
			Object[] arguments = {serviceId};
			String[] signature = {Long.class.getName()};	
			String xslt = null;
			Object result = JMXManager.callOperation(jmxServiceURL, mBeanName, "getToCanonicalXSL" , arguments, signature);
			if(result != null){
				xslt = (String) result;
				return xslt;
			}else{
				logger.log(Level.WARNING, "There is no 'to canonical' transformation defined for service " + serviceId.toString());
			}	
			
		}catch (Exception ex) {
			logger.log(Level.SEVERE, "Exception when callOperation getToCanonicalXSL" + ".", ex);
		}
		
		return null;	
	}
	
	public static String getFromCanonicalXSL(Long serviceId) {
		try{
			String jmxServiceURL = MgrConfig.getInstance().getProperty(ConfigConstants.JMXEsbAdpURL);
			String mBeanName = JMXServiceConstants.JMXServicesAdminMBean;
			Object[] arguments = {serviceId};
			String[] signature = {Long.class.getName()};	
			String xslt = null;
			Object result = JMXManager.callOperation(jmxServiceURL, mBeanName, "getFromCanonicalXSL" , arguments, signature);
			if(result != null){
				xslt = (String) result;
				return xslt;
			}else{
				logger.log(Level.WARNING, "There is no 'from canonical' transformation defined for service " + serviceId.toString());
			}	
			
		}catch (Exception ex) {
			logger.log(Level.SEVERE, "Exception when callOperation getFromCanonicalXSL" + ".", ex);
		}
		
		return null;	
	}
}
